<?php
# No Appeal & Arbitrate Function in current version.
require_once("constraint.php");
require_once("global.php");
require_once("EKUser.php");

class EKEvent {
	private $id;
	private $userId;
	private $eventType;
	private $elementType;
	private $elementId;
	private $upadteTime;
	private $docTitle;
	private $docId;
	
	function __construct($userId,$eventType,$elementType,$elementId){
		$this->userId = $_SESSION["userid"];
		$this->eventType = $eventType;
		$this->elementType = $elementType;
		$this->elementId = substr($elementId,1);
		$this->upadteTime = time();
		
		$newExp = EKUser::getAttributeById($userId,"exp") + EKEvaluation::getEvaluation($eventType,$elementType); 
		
		EKUser::updateDB($userId, "exp",
						$newExp>0? $newExp : 0 );
		if($elementType == "t"){
			if(EKTalk::getElementAttributeById($elementId,"EKTalk","talkType") == "d"){
				$this->docId = EKTalk::getOwnerIdById($elementId);
				$this->docTitle = EKDocument::getTitleById($this->docId);
			} else {
				$pId = EKTalk::getOwnerIdById($elementId);
				$this->docId = EKParagraph::getOwnerIdById($pId);
				$this->docTitle = EKDocument::getTitleById($this->docId);
			}
		} else if ($elementType == "p"){
			$this->docId = EKParagraph::getOwnerIdById($elementId);
			$this->docTitle = EKDocument::getTitleById(EKParagraph::getOwnerIdById($this->docId));
		} else if ($elementType == "d"){
			$this->docId = $elementId;
			$this->docTitle = EKDocument::getTitleById($elementId);
		}
	}
	
	static function getNewEvents(){
		$eventArray = array();
		$event = array();
		db_connect();
		$sql = "select * from ekevent order by updateTime desc";
		$result = mysql_query($sql);
		$j = 0;
		while($row = mysql_fetch_array($result)){
			if($row["elementType"] != "r"){
				$event[] = $row["id"];
				$event[] = $row["userId"];
				$event[] = $row["elementType"];
				$event[] = "_" . $row["elementId"];
				$event[] = $row["eventType"];
				$event[] = $row["updateTime"];
				$event[] = $row["docId"];
				$event[] = $row["docTitle"];
				$eventArray[] = $event;
				$event = array();
				$j ++;
			if($j > 10) break;
			}
		}
		mysql_close();
		return $eventArray;	
	}
	
	function save(){
		db_connect();
		$sql = "insert into ekevent (userId, elementType, elementId, eventType, updateTime, docId, docTitle) values 
			('" . $this->userId . "', '"
				. $this->elementType . "', '"
				. $this->elementId . "', '"
				. $this->eventType . "', '"
				. $this->upadteTime . "', '"
				. $this->docId . "', '"
				. $this->docTitle . "')";
		$result = mysql_query($sql);
	    if (!$result){
	        throw new Exception("MySQL error: ".mysql_error());
	    }
		mysql_close();
	}
	
	function getId(){
		return $this->id;
	}
	function getUserId(){
		return $this->userId;
	}
	function getEventType(){
		return $this->elementType;
	}
	function getElementId(){
		return $this->elementId;
	}
	function getElementType(){
		return $this->getElementType();
	}
	function getUpdateTime(){
		return date("Y/m/d [H:i:s]",$this->upadteTime);
	}
}
?>